package org.galaxyworld.flycode.dbspy;

import org.beetl.core.Template;
import org.galaxyworld.flycode.config.AppConfig;
import org.galaxyworld.flycode.config.ModelConfig;
import org.galaxyworld.flycode.core.FileGenerator;
import org.galaxyworld.flycode.core.TemplateManager;
import org.galaxyworld.flycode.model.Table;
import org.galaxyworld.flycode.utils.OutputFileUtils;

import java.io.Writer;

public class ModelFileGenerator implements FileGenerator {

    private Table table;

    public ModelFileGenerator(Table table) {
        this.table = table;
    }

    @Override
    public void generate() {
        System.out.println(String.format("Generating for %s...", table.getName()));
        Writer writer = OutputFileUtils.createWriter(AppConfig.getOutputDir(), ModelConfig.getPackageName(), table.getClassName());
        Template template = TemplateManager.getTemplate(ModelConfig.getTemplate());
        template.binding("packageName", ModelConfig.getPackageName());
        template.binding("tableName", table.getName());
        template.binding("className", table.getClassName());
        template.binding("fields", table.getFields());
        template.renderTo(writer);
    }

}
